package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.real8.romemp.client.GameMessages;

public class TradeAllocationViewImpl extends Composite implements
		TradeAllocationView {

	private static TradeAllocationViewImplUiBinder uiBinder = GWT
			.create(TradeAllocationViewImplUiBinder.class);

	interface TradeAllocationViewImplUiBinder extends
			UiBinder<Widget, TradeAllocationViewImpl> {
	}

	private Presenter presenter;

	@UiField
	HTMLPanel overviewPanel;
	@UiField
	SpanElement availableWarShipsSpan;
	@UiField
	SpanElement availableMerchantShipsSpan;
	@UiField
	SpanElement labourArtisansSpan;

	@UiField
	QuestionAndAnswerView merchantShipQAView;

	@UiField
	QuestionAndAnswerView warShipQAView;

	public TradeAllocationViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;

		merchantShipQAView.setPresenter(new QuestionAndAnswerView.Presenter() {
			@Override
			public void onAnswer(int answer) {
				presenter.onMerchantShipAnswer(answer);
			}
		});
		warShipQAView.setPresenter(new QuestionAndAnswerView.Presenter() {
			@Override
			public void onAnswer(int answer) {
				presenter.onWarShipAnswer(answer);
			}
		});
	}

	@Override
	public void hideAll() {
		overviewPanel.setVisible(false);
		merchantShipQAView.setVisible(false);
		warShipQAView.setVisible(false);
	}

	@Override
	public void showOverview(int availableWarShips, int availableMerchantShips,
			int labourArtisans) {
		availableWarShipsSpan.setInnerText("" + availableWarShips);
		availableMerchantShipsSpan.setInnerText("" + availableMerchantShips);
		labourArtisansSpan.setInnerText("" + labourArtisans);

		overviewPanel.setVisible(true);
	}

	@Override
	public void showMerchantShipQ() {
		GameMessages messages = (GameMessages) GWT.create(GameMessages.class);

		merchantShipQAView.showQuestion(messages.howMuchMerchantShip());
	}

	@Override
	public void showWarShipQ() {
		GameMessages messages = (GameMessages) GWT.create(GameMessages.class);

		warShipQAView.showQuestion(messages.howMuchEscortingWarShip());
	}
}
